/*
 * vws_thread.h
 *
 *  Created on: Sep 20, 2013
 *      Author: vscen
 */

#ifndef VWS_THREAD_H_
#define VWS_THREAD_H_

#include "vws_config.h"

namespace vws {
class vws_thread;
class InterfaceTP {
	virtual void DoThread(void* arg) = 0;
	virtual void DoFinal() = 0;
	friend class vws_thread;
};
typedef std::queue<InterfaceTP*> TaskQueue, *PTaskQueue;

typedef struct _THREAD_CONTEXT {
	vws_thread* pWorkQueue;
	void* pThreadData;
} THREAD_CONTEXT, *PTHREAD_CONTEXT;

typedef std::queue<PTHREAD_CONTEXT> ThreadCtxQueue, *PThreadCtxQueue;

class vws_thread {
public:
public:
	void RunWorkThread(int iMaxT, void** argv);
	void AddTaskThread(InterfaceTP* pTask);
	void DestroyThread();
private:
	InterfaceTP* SunTaskThread();
	int WaitSem();
	int PostSem();
	int LockMutex();
	int UnlockMutex();
public:
	virtual ~vws_thread() {
	}
private:
	sem_t _signal;
	pthread_mutex_t _mutex;
	int _threadmax;
	pthread_t* _threadid;
	PTaskQueue _taskqueue;
	ThreadCtxQueue _threadctx;
private:
	static void* WorkThread(void* arg);
};

}

#endif /* VWS_THREAD_H_ */
